<?php

namespace App\Http\Controllers\Front;

use App\Models\AgricGoods;
use App\Models\Reservation;
use App\Models\ReservationComment;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ReservationController extends Controller
{
    private $data = array();

    public function __construct(Request $request)
    {
        $this->data['pageTitle'] = '乡通-专业农业指导服务';
    }

    /**
     * 专业农业指导服务列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        $this->data['hotGoods'] = AgricGoods::pc_hot_goods_list(); // 农品热销
        $this->data['positionInfo'] = ['name' => '专业农业指导服务', 'url' => ''];
        return view('front.reservation.lists', $this->data);
    }

    /**
     * ajax专业农业指导列表（附带一条专家评论）
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function ajax_reservation_lists(Request $request)
    {
        $pagesize  = $request->pagesize ?? 10;
        $reservationData = Reservation::where('is_show', 1)
            ->with(['reserver', 'service_category', 'region_info'])
            ->orderBy('is_top', 'desc')
            ->orderBy('weight', 'desc')
            ->orderBy('created_at', 'desc')
            ->paginate($pagesize);

        $reservationData = $reservationData->toArray();

        foreach ($reservationData['data'] as &$reservationVal) {
            $reservationVal['expert_common'] = [];
            // 获得第一条专家评论信息
            if ($reservationVal['expert_comment_state'] == 1) {
                $expertCommon = ReservationComment::where('reservation_id', $reservationVal['id'])
                    ->where('is_expert', 1)
                    ->where('is_show', 1)
                    ->with(['replyer', 'commenter'])
                    ->orderBy('zan_num', 'desc')
                    ->orderBy('created_at', 'asc')
                    ->first();
                $reservationVal['expert_common'] = $expertCommon->toArray();
            }
        }

        $this->data['reservationData'] = $reservationData;
        return view('front.reservation.ajax_lists', $this->data);
    }

    /**
     * 专业农业指导服务详情页
     * @param Request $request
     * @param int $reservationId
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function details(Request $request, $reservationId = 0)
    {
        $reservation = Reservation::where('is_show', 1)->with(['reserver', 'service_category', 'region_info'])->find($reservationId);

        // 获得评论内容 （专家内容置顶）
        $commonList = ReservationComment::where('reservation_id', $reservationId)
            ->where('is_show', 1)
            ->with(['replyer', 'commenter'])
            ->orderBy('is_expert', 'desc')
            ->orderBy('created_at', 'asc')
            ->get();

        $this->data['reservation'] = $reservation;
        $this->data['commonList'] = $commonList;
        $this->data['positionInfo'] = [
            ['name' => '专业农业指导服务', 'url' => url('reservation')],
            ['name' => '详情', 'url' => '']
        ];
        return view('front.reservation.details', $this->data);
    }

}
